package org.liuhy
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Alert;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.core.DragSource;
	import mx.core.UIComponent;
	import mx.managers.DragManager;
	
	public class ULine extends UIComponent
	{
		public var x1:int;
		public var y1:int;
		public var x2:int;
		public var y2:int;
		public var preLine:ULine 			= null;	//前一条线段;
		public var nextLine:ULine			= null;	//后一条线段;
		
		//
		public var in_flowNode:FlowNode 	= null;		//流程节点,入线口

		//
		public var out_flowNode:FlowNode 	= null;		//流程节点,出线口
		
		public var fromLineNode		:LineNode 	= null;			//线段开始Focus点;
		public var midLineNode		:LineNode 	= null;			//线段中间Focus点;
		public var arrowLineNode	:LineNode 	= null;				//线段最后一条险段箭头Focus点;
		public var _arrow:Arrow					= null;				//线段箭头;
		private var old_x1:int;
		private var old_y1:int;
		private var old_x2:int;
		private var old_y2:int;
		private var text:Text = new Text;
		public var app:Application;
		public  function ULine(p_x1:int,p_y1:int,p_x2:int,p_y2:int,p_app:Application)
		{
			this.x1  = p_x1;
			this.y1  = p_y1;
			this.x2  = p_x2;
			this.y2  = p_y2;
			this.app = p_app;
			this.old_x1 = this.x1;
			this.old_y1 = this.y1;
			this.old_x2 = this.x2;
			this.old_y2 = this.y2;
			this.width = 20;
			this.setStyle("color","#ffccee");
			this.midLineNode = new MidLineNode(p_x1 + (p_x2-p_x1)/2, p_y1+(p_y2-p_y1)/2-5);
			this.fromLineNode = new FromLineNode(p_x1,p_y1);
//			this.midNode.visible = false;
//			this.fromNode.visible = false;
			this.addChild(midLineNode);
			this.addChild(fromLineNode);
			
			midLineNode.addEventListener(MouseEvent.MOUSE_DOWN,midLineNode.startDragging);
			midLineNode.addEventListener(MouseEvent.MOUSE_UP,midLineNode.stopDragging);
//			
//			fromNode.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
			fromLineNode.addEventListener(MouseEvent.MOUSE_UP,fromLineNode.stopDragging);			
			fromLineNode.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			
//			
//			this.addEventListener(MouseEvent.CLICK,onClick);
//			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.app.addChild(this);
		}
		private function onMouseMove(event:MouseEvent):void{
			var dragInitiator:Node=Node(event.currentTarget);
            var ds:DragSource = new DragSource();
            ds.addData(this,"line");
            DragManager.doDrag(dragInitiator, ds, event);
		}
		
		private function onMouseOver(event:MouseEvent):void{
//				this.fromNode.visible = true;
//			this.midNode.visible = true;
//			if(null!=this.nextLine){
//				this.nextLine.fromNode.visible = true;
//			}
		}
		
		private function onClick(event:MouseEvent):void{
			this.fromLineNode.visible = true;
			this.midLineNode.visible = true;
			if(null!=this.nextLine){
				this.nextLine.fromLineNode.visible = true;
			}
		}
		
		private function startDragging(event:MouseEvent):void{
			event.currentTarget.startDrag();
			
		}
		private function stopDragging(event:MouseEvent):void{
			var _obj:Node = event.currentTarget as Node;
			event.currentTarget.stopDrag();		
			reMidDrawLine(_obj);	
		}
		
//		private function _fromDragging(event:MouseEvent):void{
//			event.currentTarget.startDrag();
//		}
		private function _toDragging(event:MouseEvent):void{
			
			var _obj:Node = event.currentTarget as Node;
			event.currentTarget.stopDrag();		
			_reFromToDrawLine(_obj);	
		}
//		private function _fromDragging(event:MouseEvent):void{
//			event.currentTarget.startDrag();
//		}
		private function _arrowDragging(event:MouseEvent):void{
			var _obj:Node = event.currentTarget as Node;
			event.currentTarget.stopDrag();		
			_reArrowDrawLine(_obj);	
		}
		public function _arrowLeftFocus(p_obj:Node,_fn:FlowNode):void{			
			
		}
		public function _arrowRightFocus(p_obj:Node,_fn:FlowNode):void{			
			//右侧连接
			if(this.x1 < (_fn.x + this.in_flow_focusNode.x)){
				//1.创建左侧线段
				var _line1:ULine = new ULine(p_obj.x+40,p_obj.y,p_obj.x,p_obj.y,this.app);
				if(null!=this.preLine){
					//上一根线；
					if(null!=this.preLine.preLine){
						//只移动this.preLine;
						var _line2:ULine = new ULine(_line1.x1,this.preLine.y1,_line1.x1,_line1.y1,this.app);
						
						_line1.preLine = _line2;
						_line2.nextLine = _line1;
						_line2.preLine = this.preLine.preLine;
						this.preLine.preLine.x2 = _line2.x1;
						this.preLine.preLine.nextLine = _line2;
						this.preLine.preLine.drawLine();
						_line1.drawLine();
						_line2.drawLine();									
						LineUtils.removeLine(this.preLine);
						LineUtils.removeLine(this);
						_line1.in_flow_focusNode = _fn._right_node;
						_fn._outLine = _line1;	
					}else{
						var _y2:int = this.preLine.y1 + (p_obj.y-this.preLine.y1)/2;//
						var _line2:ULine = new ULine(_line1.x1,_y2,_line1.x1,_line1.y1,this.app);								
						var _line3:ULine = new ULine(this.preLine.x1,_line2.y1,_line2.x1,_line2.y1,this.app);
						var _line4:ULine = new ULine(this.preLine.x1,this.preLine.y1,_line3.x1,_line3.y1,this.app);
						
						_line1.preLine 	= _line2;
						_line2.nextLine = _line1;
						_line2.preLine 	= _line3;
						_line3.nextLine = _line2;
						_line3.preLine 	= _line4;
						_line4.nextLine = _line3;
						_line4.out_flow_focusNode = this.preLine.out_flow_focusNode;
						LineUtils.removeLine(this.preLine);	
						LineUtils.removeLine(this);	
						_line1.drawLine();
						_line2.drawLine();
						_line3.drawLine();
						_line4.drawLine();	
						_line1.in_flow_focusNode = _fn._right_node;
						_fn._outLine = _line1;	
					}
				}else{
					//单根直线;
					Alert.show("单根直线");
				}
					
			}else{
				
				//箭头节点;
				
				this.x2 = p_obj.x;
				this.y2 = p_obj.y;
				this.y1 = p_obj.y;
				this.drawLine();
				if(null!=this.preLine){
					this.preLine.x2 = this.x1;
					this.preLine.y2 = this.y1;
					this.preLine.x1 = this.x1;								
					this.preLine.drawLine();
					if(null!=this.preLine.preLine){
						this.preLine.preLine.x2 = this.preLine.x1;
						this.preLine.preLine.y2 = this.preLine.y1;
						this.preLine.preLine.drawLine();
					}								
				}
				if(null!=this.preLine && null!=this.nextLine){
					this.nextLine.x1 = this.x2;
					this.nextLine.y1 = this.y2;
					this.nextLine.drawLine();
				}
				if(null==this.preLine && null!=this.nextLine){
					this.nextLine.x1 = this.x2;
					this.nextLine.y1 = this.y2;
					this.nextLine.drawLine();
				}	
			}
		}
		
		//箭头拖动
		public  function _reArrowDrawLine(p_obj:LineNode):void{
			//横线
			if(this.y1 == this.y2){			
				if(null!=this.in_flow_focusNode){
					//连接流程节点;
					var _fn:FlowNode = this.in_flow_focusNode.parent as FlowNode;
					if("LEFT"==this.in_flow_focusNode._flow_node_type){
						this._arrowLeftFocus(p_obj,_fn);
					}else if("RIGHT"==this.in_flow_focusNode._flow_node_type)	{
						Alert.show("h_to_right");
						this._arrowRightFocus(p_obj,_fn);
					}else if("TOP"==this.in_flow_focusNode._flow_node_type)	{
						
					}else if("BOTTOM"==this.in_flow_focusNode._flow_node_type)	{
						
					}				
				}else{	
					//箭头节点;
					this.x2 = p_obj.x;
					this.y2 = p_obj.y;
	//				this.x1 = p_obj.x;
					this.y1 = p_obj.y;
					this.drawLine();
					if(null!=this.preLine){
						this.preLine.x2 = this.x1;
						this.preLine.y2 = this.y1;
						this.preLine.x1 = this.x1;
						
						this.preLine.drawLine();
						if(null!=this.preLine.preLine){
							this.preLine.preLine.x2 = this.preLine.x1;
							this.preLine.preLine.y2 = this.preLine.y1;
							this.preLine.preLine.drawLine();
						}
						
					}
					if(null!=this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}
					if(null==this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}	
				}
			}
			//竖线
			if(this.x1 == this.x2){
				if(null==this.preLine && null==this.nextLine){
//					this.x1 = p_obj.x;
//					this.y1 = p_obj.y;
					var _y2:int = this.y1 + (p_obj.y-this.y1)/2;//
					var _line1:ULine = new ULine(this.x1,this.y1,this.x1,_y2,this.app);	
									
					var _line2:ULine = new ULine(_line1.x2,_line1.y2,p_obj.x,_y2,this.app);
					_line1.nextLine = _line2;
					_line2.preLine = _line1;					
//					var _y3:int = this.old_y1 + 3*(this.old_y2-this.old_y1)/4;//	
										
					var _line3:ULine = new ULine(_line2.x2,_line2.y2,p_obj.x,p_obj.y,this.app);
					_line2.nextLine = _line3;
					_line3.preLine = _line2;
					LineUtils.removeLine(this);
					_line1.drawLine();
					_line2.drawLine();
					_line3.drawLine();					
				}else{
					//from节点;
					this.x2 = p_obj.x;
					this.y2 = p_obj.y;
					this.x1 = p_obj.x;
	//				this.y2 = p_obj.y;
					this.drawLine();
					if(null!=this.preLine){
						this.preLine.x2 = this.x1;
						this.preLine.y2 = this.y1;
						this.preLine.y1 = this.y1;
						
						this.preLine.drawLine();
						if(null!=this.preLine.preLine){
							this.preLine.preLine.x2 = this.preLine.x1;
							this.preLine.preLine.y2 = this.preLine.y1;
							this.preLine.preLine.drawLine();
						}
						
					}
					if(null!=this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}
					if(null==this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}
				}
			}
		}
		//开始节点拖动;
		public function _reFromToDrawLine(p_obj:Node):void{			
			//横线
			if(this.y1 == this.y2){
				//from节点;
				this.x1 = p_obj.x;
				this.y1 = p_obj.y;
//				this.x2 = p_obj.x;
				this.y2 = p_obj.y;
				this.drawLine();
				if(null!=this.preLine){
					this.preLine.x2 = this.x1;
					this.preLine.y2 = this.y1;
					this.preLine.x1 = this.x1;
					
					this.preLine.drawLine();
					if(null!=this.preLine.preLine){
						this.preLine.preLine.x2 = this.preLine.x1;
						this.preLine.preLine.y2 = this.preLine.y1;
						this.preLine.preLine.drawLine();
					}
					
				}
				if(null!=this.preLine && null!=this.nextLine){
					this.nextLine.x1 = this.x2;
					this.nextLine.y1 = this.y2;
					this.nextLine.drawLine();
				}
				if(null==this.preLine && null!=this.nextLine){
					this.nextLine.x1 = this.x2;
					this.nextLine.y1 = this.y2;
					this.nextLine.drawLine();
				}
			}
			//竖线
			if(this.x1 == this.x2){
				//
				if(null==this.preLine && null==this.nextLine){
					this.x1 = p_obj.x;
					this.y1 = p_obj.y;
					var _y2:int = this.y1 + (this.y2-this.y1)/2;//
					var _line1:ULine = new ULine(this.x1,this.y1,this.x1,_y2,this.app);	
									
					var _line2:ULine = new ULine(_line1.x2,_line1.y2,this.x2,_y2,this.app);
					_line1.nextLine = _line2;
					_line2.preLine = _line1;					
//					var _y3:int = this.old_y1 + 3*(this.old_y2-this.old_y1)/4;//						
					var _line3:ULine = new ULine(_line2.x2,_line2.y2,this.x2,this.y2,this.app);
					_line2.nextLine = _line3;
					_line3.preLine = _line2;
					LineUtils.removeLine(this);
					_line1.drawLine();
					_line2.drawLine();
					_line3.drawLine();					
				}else{
//				//from节点;
					this.x1 = p_obj.x;
					this.y1 = p_obj.y;
					this.x2 = p_obj.x;
					this.drawLine();
					if(null!=this.preLine){
						this.preLine.x2 = this.x1;
						this.preLine.y2 = this.y1;
						this.preLine.y1 = this.y1;
						
						this.preLine.drawLine();
						if(null!=this.preLine.preLine){
							this.preLine.preLine.x2 = this.preLine.x1;
							this.preLine.preLine.y2 = this.preLine.y1;
							this.preLine.preLine.drawLine();
						}
						
					}
					if(null!=this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}
					if(null==this.preLine && null!=this.nextLine){
						this.nextLine.x1 = this.x2;
						this.nextLine.y1 = this.y2;
						this.nextLine.drawLine();
					}
				}
			}
		}
		public function reMidDrawLine(p_obj:Node):void{			
//			向左右移动
			if(this.x1==this.x2){
				
				this.x1 		= p_obj.x;
				this.x2 		= p_obj.x;
				this.drawLine();
				//向左拉动
				if(null!=this.preLine){
					this.preLine.x2 = p_obj.x;
					this.preLine.drawLine();
				}
				if(null!=this.nextLine){
					this.nextLine.x1 = p_obj.x;						
					this.nextLine.drawLine();
				}
			}
//			向下移动
			if(this.y1==this.y2){
				//如果相连线为直线,则分取中间做折线
//				if(null==this.preLine && null==this.nextLine){
//
//				}else{
					//向下拉动
					if(null!=this.preLine){
//						this.preLine.y1 = p_obj.y;
						this.preLine.y2 = p_obj.y;
						this.y1 		= p_obj.y;
						this.y2 		= p_obj.y;
						
						this.drawLine();
						this.preLine.drawLine();
					}
					if(null!=this.nextLine){
						this.nextLine.y1 = p_obj.y;
						
						this.nextLine.drawLine();
						this.y1 = p_obj.y;
						this.y2 = p_obj.y;
						this.drawLine();
					}
//				}				
			}else{
				
			}
			//合并距离过近的线段;
			LineUtils.toOneLine(this);
		}
	
		public function drawLine():void{			
			if(x1==x2){
				//垂直
				this.fromLineNode.x = x1;
				this.fromLineNode.y = y1;
				if(y1<y2){
					this.midLineNode.x = x1;
					this.midLineNode.y = y1 + (y2-y1)/2;
				}else{
					this.midLineNode.x = x1;
					this.midLineNode.y = y2 + (y1-y2)/2;
				}
			}else if(y1==y2){
				//水平
				this.fromLineNode.x = x1;
				this.fromLineNode.y = y1;
				if(x1<x2){					
					this.midLineNode.x = x1 + (x2-x1)/2;
					this.midLineNode.y = y1;
				}else{
					this.midLineNode.x = x2 + (x1-x2)/2;
					this.midLineNode.y = y1;
				}
			
			}
			graphics.clear();
			graphics.moveTo(x1,y1);
			graphics.beginFill(0x00FF00);
			graphics.lineTo(x2, y2);
			graphics.lineStyle(1, 0x000000);

			graphics.endFill();
			if(null!=this._arrow){
				this.app.removeChild(this._arrow);
				this.app.removeChild(this.arrowLineNode);
			}
			if(null==this.nextLine){
				//末端增加箭头
				_arrow 				= 	new Arrow();
				var toPoint:Point	=	new Point(this.x2,this.y2);
				var fromPoint:Point = 	new Point(this.x1,this.y1);
                _arrow.fromPoint	=	fromPoint;
                _arrow.toPoint		=	toPoint;
                _arrow.draw();
                this.app.addChild(_arrow);
                //末端增加调整方块
                arrowLineNode = new ArrowLineNode(this.x2,this.y2);
//                toNode.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
//				toNode.addEventListener(MouseEvent.MOUSE_UP,_arrowDragging);
				arrowLineNode.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
//				arrowLineNode._line_node_type = "T";
				this.app.addChild(arrowLineNode);
			}
		}
	}
}